package com.hsy.swu.other

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.hsy.swu.databinding.FragmentOtherBinding
import com.hsy.swu.hsyutils.FragmentAdapter
import com.hsy.swu.hsyutils.HsyFragment
import com.hsy.swu.hsyutils.HsyViewModel
import com.hsy.swu.hsyutils.setFont
import com.hsy.swu.other.game2048.Game2048Fragment
import com.hsy.swu.other.idleclassroom.IdleClassRoomFragment

class OtherFragment : HsyFragment<FragmentOtherBinding, HsyViewModel>() {
    private val tab1Name = "2048"
    private val tab2Name = "空闲教室"
    private val tab3Name = "敬请期待"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewPager2()
    }

    private fun setViewPager2() {
        val otherPageAdapter = FragmentAdapter(childFragmentManager, lifecycle)
        otherPageAdapter.setPageList(
            listOf(
                Game2048Fragment(),
                IdleClassRoomFragment(),
//                IdleClassRoomFragment()
            )
        )
        mBinding.viewPager2.adapter = otherPageAdapter
        mBinding.viewPager2.isSaveEnabled = false

        TabLayoutMediator(mBinding.tabLayout, mBinding.viewPager2) { tab, position ->
            when (position) {
                0 -> tab.text = tab1Name
                1 -> tab.text = tab2Name
//                2 -> tab.text = tab3Name
            }
        }.attach()

        mBinding.viewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                mBinding.tabLayout.setFont()
            }
        })

        mBinding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            @SuppressLint("ResourceType")
            override fun onTabSelected(tab: TabLayout.Tab?) {
                mBinding.tabLayout.setFont()
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })
    }
}